package com.hzy.quetosys.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzy.quetosys.dao.CustomerMapper;
import com.hzy.quetosys.entity.Customer;
import com.hzy.quetosys.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Transactional(rollbackFor =Exception.class)
    @Override
    public boolean append(Customer customer) {
        return customerMapper.insert(customer) > 0;
    }

    @Override
    public PageInfo<Customer> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        final List<Customer> customers = customerMapper.selectAll();
        PageInfo<Customer> pageInfo=new PageInfo<>(customers);
        return pageInfo;
    }

    @Override
    public PageInfo<Customer> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer> customers=customerMapper.selectByKeyword(keyword);
        PageInfo<Customer> pageInfo=new PageInfo<>(customers);
        return pageInfo;
    }

    @Override
    public Customer findById(int id) {
        return customerMapper.selectById(id);
    }

    @Transactional(rollbackFor =Exception.class)
    @Override
    public boolean modify(Customer customer) {
        return customerMapper.updateById(customer)>0;
    }

    @Transactional(rollbackFor =Exception.class)
    @Override
    public boolean remove(int id) {
        return customerMapper.deleteById(id)>0;
    }
}
